﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KenLib.Common;
using KenLib.BusinessManager;
using DataInfo;
using System.Collections.Generic;

public partial class APP_MasterPage_appCom : System.Web.UI.MasterPage
{
    本社情報Manager _本社情報Manager = new 本社情報Manager();
    基本情報Manager _基本情報Manager = new 基本情報Manager();
    public int index = 0;
    public int year = 0;
    string strDivsName = "div_frmA_OtherOrgTraineeList,div_input,div_frmA_frmA_MCompManager1,div_frmA_CompBranchHistoryCheck,div_frmA_CompBranchHistoryInput,div_frmA_CompBasicInfoCheck,div_frmA_CompHistoryCheck,div_frmA_CompBasicInfoInput,div_frmA_CompHistoryInput,div_frmA_CompBranchInput,div_frmA_CompBranchCheck,div_frmA_TraineeBasicInfo,div_frmA_TraineeBasicInfo1,div_frmA_TraineeList,div_frmA_JointCompBasicInfoRef,div_frmA_OrgBasicInfoRef,div_frmA_CompBranchList,div_frmA_MOtherOrgRef,div_frmA_MCompManager,div_frmA_CompBasicInfo,div_frmA_CompBranchInfo,div_frmA_CompHistoryInfo,div_frmA_CompBasicInfo1,div_frmA_CompBranchInfo1,div_frmA_CompHistoryInfo1,div_frmA_OrgTraineeInfoInput";
    /// <summary>
    /// Coder: Dong
    /// Noi dung: Hien thi ten va quyen dang nhap len master pager
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["frmD_Login_user"] != null && Session["frmD_Login_user"].ToString() != "")
        {
            if (Session["frmD_Login_user"].ToString() == "4")
            {
                if (Session["frmD_Login_企業コード"] != null && Session["frmD_Login_企業コード"].ToString() != "")
                {
                    DataTable dt1 = new DataTable();
                    dt1 = _本社情報Manager.frmD_UserLoginManager_Select企業ID_本社情報(Session["frmD_Login_企業コード"].ToString());
                    if (dt1.Rows.Count > 0)
                    {
                        lbl_User.Text = ShowTextLength(dt1.Rows[0]["企業名"].ToString());
                        lbl_User.ToolTip = dt1.Rows[0]["企業名"].ToString();
                    }
                    else
                    {
                        lbl_User.Text = "";
                    }
                }
                else
                {
                    Response.Redirect("frmD_Login.aspx");
                }
            }
            else if (Session["frmD_Login_user"].ToString() == "5")
            {
                lbl_User.Text = "Administrator";
                lbl_User.ForeColor = System.Drawing.Color.Yellow;
            }
            else if (Session["frmD_Login_user"].ToString() == "6")
            {
                DataTable dt1 = new DataTable();
                DataTable dt2 = new DataTable();
                if (Session["frmD_Login_企業コード"] != null && Session["frmD_Login_企業コード"].ToString() != "" && Session["frmD_Login_人コード"] != null && Session["frmD_Login_人コード"].ToString() != "")
                {
                    dt1 = _本社情報Manager.frmD_UserLoginManager_Select企業ID_本社情報(Session["frmD_Login_企業コード"].ToString());
                    dt2 = _基本情報Manager.frmD_Login_Get社員ID(Convert.ToInt32(dt1.Rows[0]["企業ID"]), Session["frmD_Login_人コード"].ToString());
                    if (dt1.Rows.Count > 0 && dt2.Rows.Count > 0)
                    {
                        lbl_User.Text = ShowTextLength(dt2.Rows[0]["姓名"].ToString() + "・" + dt1.Rows[0]["企業名"].ToString());
                        lbl_User.ToolTip = dt2.Rows[0]["姓名"].ToString() + "・" + dt1.Rows[0]["企業名"].ToString();
                        lbl_User.ForeColor = System.Drawing.Color.Yellow;
                    }
                    else
                    {
                        Response.Redirect("frmD_Login.aspx");
                    }
                }
            }
            else if (Session["frmD_Login_user"].ToString() == "7")
            {
                DataTable dt1 = new DataTable();
                DataTable dt2 = new DataTable();
                if (Session["frmD_Login_企業コード"] != null && Session["frmD_Login_企業コード"].ToString() != "" && Session["frmD_Login_人コード"] != null && Session["frmD_Login_人コード"].ToString() != "")
                {
                    dt1 = _本社情報Manager.frmD_UserLoginManager_Select企業ID_本社情報(Session["frmD_Login_企業コード"].ToString());
                    dt2 = _基本情報Manager.frmD_Login_Get社員ID(Convert.ToInt32(dt1.Rows[0]["企業ID"]), Session["frmD_Login_人コード"].ToString());
                    if (dt1.Rows.Count > 0 && dt2.Rows.Count > 0)
                    {
                        lbl_User.Text = ShowTextLength(dt2.Rows[0]["姓名"].ToString() + "・" + dt1.Rows[0]["企業名"].ToString());
                        lbl_User.ToolTip = dt2.Rows[0]["姓名"].ToString() + "・" + dt1.Rows[0]["企業名"].ToString();
                    }
                    else
                    {
                        Response.Redirect("frmD_Login.aspx");
                    }
                }
            }
            else if (Session["frmD_Login_user"].ToString() == "8")
            {
                DataTable dt1 = new DataTable();
                DataTable dt2 = new DataTable();
                if (Session["frmD_Login_企業コード"] != null && Session["frmD_Login_企業コード"].ToString() != "" && Session["frmD_Login_人コード"] != null && Session["frmD_Login_人コード"].ToString() != "")
                {
                    dt1 = _本社情報Manager.frmD_UserLoginManager_Select企業ID_本社情報(Session["frmD_Login_企業コード"].ToString());
                    dt2 = _基本情報Manager.frmD_Login_Get社員ID(Convert.ToInt32(dt1.Rows[0]["企業ID"]), Session["frmD_Login_人コード"].ToString());
                    if (dt1.Rows.Count > 0 && dt2.Rows.Count > 0)
                    {
                        lbl_User.Text = ShowTextLength(dt2.Rows[0]["姓名"].ToString() + "・" + dt1.Rows[0]["企業名"].ToString());
                        lbl_User.ToolTip = dt2.Rows[0]["姓名"].ToString() + "・" + dt1.Rows[0]["企業名"].ToString();
                        lbl_User.ForeColor = System.Drawing.Color.Yellow;
                    }
                    else
                    {
                        Response.Redirect("frmD_Login.aspx");
                    }
                }
            }
            else if (Session["frmD_Login_user"].ToString() == "9")
            {
                DataTable dt1 = new DataTable();
                DataTable dt2 = new DataTable();
                if (Session["frmD_Login_企業コード"] != null && Session["frmD_Login_企業コード"].ToString() != "" && Session["frmD_Login_人コード"] != null && Session["frmD_Login_人コード"].ToString() != "")
                {
                    dt1 = _本社情報Manager.frmD_UserLoginManager_Select企業ID_本社情報(Session["frmD_Login_企業コード"].ToString());
                    dt2 = _基本情報Manager.frmD_Login_Get社員ID(Convert.ToInt32(dt1.Rows[0]["企業ID"]), Session["frmD_Login_人コード"].ToString());
                    if (dt1.Rows.Count > 0 && dt2.Rows.Count > 0)
                    {
                        lbl_User.Text = ShowTextLength(dt2.Rows[0]["姓名"].ToString() + "・" + dt1.Rows[0]["企業名"].ToString());
                        lbl_User.ToolTip = dt2.Rows[0]["姓名"].ToString() + "・" + dt1.Rows[0]["企業名"].ToString();
                    }
                    else
                    {
                        Response.Redirect("frmD_Login.aspx");
                    }
                }
            }
            else
            {
                Response.Redirect("frmD_Login.aspx");
            }
        }
        else
        {
            //Response.Redirect("frmD_Login.aspx");
            lbl_User.Text = "";
        }




       

        if (!IsPostBack)
        {
            //ddl_年月日.DataSource = ls2;
            ddl_年月日.DataBind();
            年月日frmA_CompBasicInfo();
            年月日frmA_CompHistoryInfo();
            this.CompBranchHistory();
            this.ShowYearHistoryInfo();
            this.Show拠点情報();
            this.Show拠点情報CompBranchInfo();
            this.Show拠点情報1CompBranchInfo();
            this.Showddl_送出し機関情報_CompBranchList();
            this.Showddl_本社情報_OrgBasicInfoRef();
        }

        //Coder: Dong
        //Date: 2012/02/13
        //hpl_メインメニュー.Attributes.Add("onclick", "if(confirm('" + GetError("CF00002") + "')== false) return false;");

    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Cut string when string too long
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    /// 



    public void CompBranchHistory()
    {
        List<string> ls2 = new List<string>();
       
        int year = DateTime.Now.Year;
        int lastyear = year - 10;
        for (int i = lastyear; i <= year; i++)
        {
            ls2.Insert(0, i.ToString() + "年3月1日");

        }
        ddl_年月日.DataSource = ls2;
        ddl_年月日.DataBind();
        ListItem item = new ListItem("[選択]", "-1");
        item.Selected = true;
        ddl_年月日.Items.Insert(0, item);
    }
    public string ShowTextLength(string temp)
    {

        if (checkContainsUnicodeCharacter(temp))
        {
            //true tieng nhat
            if (temp.Length > 50)
            {
                temp = temp.Substring(0, 50) + "...";
            }
        }
        else
        {
            //tieng viet
            if (temp.Length > 80)
            {
                temp = temp.Substring(0, 80) + "...";
            }
        }

        return temp;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Check byte string
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public bool checkContainsUnicodeCharacter(string input)
    {
        const int MaxAnsiCode = 255;
        char[] a = input.ToCharArray();
        for (int i = 0; i < a.Length; i++)
        {
            if (a[i] > MaxAnsiCode)
            {
                return true;
            }
        }
        return false;
    }

    // Coder: Dong
    // Date: 2012/02/13
    public String GetError(string errorid)
    {
        string error = "";
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        if (dt.Rows.Count > 0)
        {
            error = dt.Rows[0][2].ToString();
        }
        return error;
    }

    public void changeTitle(string value)
    {
        lbl_titleHeader.Text = value;
    }
    public void HideLiArea(string divShowName)
    {
        string[] str = strDivsName.Split(',');
        for (int i = 0; i < str.Length; i++)
        {
            Control c = aaa.FindControl(str[i]);
            if (c != null)
                c.Visible = false;
        }
        Control c1 = aaa.FindControl("div_" + divShowName);
        if (c1 != null)
            c1.Visible = true;
        Control c2 = aaa.FindControl("div_" + divShowName + "1");
        if (c2 != null)
            c2.Visible = true;

    }


    protected void lbtnLogout_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Response.Redirect("~/frmD_Login.aspx");
    }

    public void 年月日frmA_CompBasicInfo()
    {
        int year = DateTime.Now.Year;
        int month = DateTime.Now.Month;
        if (month > 3)// 3 o day la nam tai chinh cua cong ty .  cai nay co the lay tu database len de so sanh
        {
            year = year - 1;
        }
        else
        {
            year = year - 2;
        }
        int lastyear = year - 9;
        List<string> ls2 = new List<string>();
        for (int i = lastyear; i <= year; i++)
        {
            ls2.Insert(0, i.ToString() + "年度");
        }
        ddl_年月日frmA_CompBasicInfo.DataSource = ls2;
        ddl_年月日frmA_CompBasicInfo.DataBind();
        ListItem item = new ListItem("[選択]", "-1");
        item.Selected = true;
        ddl_年月日frmA_CompBasicInfo.Items.Insert(0, item);
    }

    public void 年月日frmA_CompHistoryInfo()
    {
        int year = DateTime.Now.Year;
        int month = DateTime.Now.Month;
        if (month > 3)// 3 o day la nam tai chinh cua cong ty .  cai nay co the lay tu database len de so sanh
        {
            year = year - 1;
        }
        else
        {
            year = year - 2;
        }
        int lastyear = year - 9;
        List<string> ls2 = new List<string>();
        for (int i = lastyear; i <= year; i++)
        {
            ls2.Insert(0, i.ToString() + "年度");
        }
        ddl_年月日frmA_CompHistoryInfo.DataSource = ls2;
        ddl_年月日frmA_CompHistoryInfo.DataBind();
        //ListItem item = new ListItem("-選択-", "-1");
        //item.Selected = true;
        //ddl_年月日frmA_CompHistoryInfo.Items.Insert(0, item);
    }


    protected void ddl_年月日frmA_CompBasicInfo_SelectedIndexChanged(object sender, EventArgs e)
    {
        string year = ddl_年月日frmA_CompBasicInfo.SelectedValue.ToString().Substring(0, 4);
        string url = "frmA_CompHistoryInfo.aspx?";
        url += "Year=" + year;

        Response.Redirect(url);
    }
    public void ShowYearHistoryInfo()
    {
        string year = Request.QueryString["Year"];
        ddl_年月日frmA_CompHistoryInfo.SelectedValue = year + "年度";

    }
    protected void ddl_年月日frmA_CompHistoryInfo_SelectedIndexChanged(object sender, EventArgs e)
    {
        string year = ddl_年月日frmA_CompHistoryInfo.SelectedValue.ToString().Substring(0, 4);
        string url = "frmA_CompHistoryInfo.aspx?";
        url += "Year=" + year;

        Response.Redirect(url);
    }
    public void Show拠点情報()
    {
        try
        {
            int 企業ID = Int32.Parse(Session["企業コード_2"].ToString());//Day la ma cua cong ty khi dang nhap vao he thong
            拠点情報Manager frm = new 拠点情報Manager();
            DataTable dt = new DataTable();
            dt = frm.Get拠点情報(企業ID);
            if (dt.Rows.Count > 0)
            {
                ddl_拠点情報.DataSource = dt;
                ddl_拠点情報.DataTextField = "拠点名";
                ddl_拠点情報.DataValueField = "拠点ID";

                ddl_拠点情報.DataBind();
                ListItem item = new ListItem("[選択]", "-1");
                item.Selected = true;
                ddl_拠点情報.Items.Insert(0, item);
            }
        }
        catch { }
    }
    public void Show拠点情報CompBranchInfo()
    {
        try
        {
            int 企業ID = Int32.Parse(Session["企業コード_2"].ToString());//Day la ma cua cong ty khi dang nhap vao he thong
            拠点情報Manager frm = new 拠点情報Manager();
            DataTable dt = new DataTable();
            dt = frm.Get拠点情報(企業ID);
            if (dt.Rows.Count > 0)
            {
                ddl_拠点情報CompBranchInfo.DataSource = dt;
                ddl_拠点情報CompBranchInfo.DataTextField = "拠点名";
                ddl_拠点情報CompBranchInfo.DataValueField = "拠点ID";
                ddl_拠点情報CompBranchInfo.DataBind();

            }

        }
        catch { }
    }
    protected void ddl_拠点情報_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            string ID = ddl_拠点情報.SelectedValue.ToString();
            //string company = ddl_拠点情報.SelectedItem.Text.ToString();
            string url = "frmA_CompBranchInfo.aspx?";
            url += "ID=" + ID;
            Response.Redirect(url);
        }
        catch { }
    }
    public void Show拠点情報1CompBranchInfo()
    {
        string ID = Request.QueryString["ID"];
        ddl_拠点情報CompBranchInfo.SelectedValue = ID;

    }
    protected void ddl_拠点情報CompBranchInfo_SelectedIndexChanged(object sender, EventArgs e)
    {
        string ID = ddl_拠点情報CompBranchInfo.SelectedValue.ToString();
        //string company = ddl_拠点情報.SelectedItem.Text.ToString();
        string url = "frmA_CompBranchInfo.aspx?";
        url += "ID=" + ID;
        Response.Redirect(url);
    }


    public void Showddl_送出し機関情報_CompBranchList()
    {
        try
        {
            int 企業ID = Int32.Parse(Session["企業コード_2"].ToString());//Day la ma cua cong ty khi dang nhap vao he thong
            本社情報Manager frm = new 本社情報Manager();
            DataTable dt = new DataTable();
            dt = frm.Get本社情報_CompBranchList(企業ID);
            ddl_送出し機関情報_CompBranchList.DataSource = dt;
            ddl_送出し機関情報_CompBranchList.DataTextField = "企業名";
            ddl_送出し機関情報_CompBranchList.DataValueField = "企業ID";
            ddl_送出し機関情報_CompBranchList.DataBind();
            ListItem item = new ListItem("[選択]", "-1");
            item.Selected = true;
            ddl_送出し機関情報_CompBranchList.Items.Insert(0, item);

        }
        catch { }
    }
    protected void ddl_送出し機関情報_CompBranchList_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            string ID = ddl_送出し機関情報_CompBranchList.SelectedValue;
            string url = "frmA_JointCompBasicInfoRef.aspx?";
            url += "ID=" + ID;
            Response.Redirect(url);
        }
        catch { }
    }


    public void Showddl_本社情報_OrgBasicInfoRef()
    {
        try
        {
            int 企業ID = Int32.Parse(Session["企業コード_2"].ToString());//Day la ma cua cong ty khi dang nhap vao he thong
            本社情報Manager frm = new 本社情報Manager();
            DataTable dt = new DataTable();
            dt = frm.Get本社情報_frmA_JointCompBasicInfoRef(企業ID);
            ddl_frmA_OrgBasicInfoRef.DataSource = dt;
            ddl_frmA_OrgBasicInfoRef.DataTextField = "企業名";
            ddl_frmA_OrgBasicInfoRef.DataValueField = "企業ID";
            ddl_frmA_OrgBasicInfoRef.DataBind();
            ListItem item = new ListItem("[選択]", "-1");
            item.Selected = true;
            ddl_frmA_OrgBasicInfoRef.Items.Insert(0, item);

        }
        catch { }
    }
    protected void ddl_frmA_OrgBasicInfoRef_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            string ID = ddl_frmA_OrgBasicInfoRef.SelectedValue;
            string url = "frmA_JointCompBasicInfoRef.aspx?";
            url += "ID=" + ID;
            Response.Redirect(url);
        }
        catch { }
    }
}
